package com.jbit.userserver.service.impl;

import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jbit.common.BusinessConstance;
import com.jbit.common.StringUtils;
import com.jbit.userserver.mapper.FunctionMapper;
import com.jbit.userserver.model.dto.FunctionDTO;
import com.jbit.userserver.model.entity.Function;
import com.jbit.userserver.model.entity.UserBusiness;
import com.jbit.userserver.service.FunctionService;
import com.jbit.userserver.service.UserBusinessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FunctionServiceImpl implements FunctionService {
    @Resource
    private FunctionMapper functionMapper;
    @Resource
    private UserBusinessService businessService;
    @Override
    public String getUrlById(String id) {
        return functionMapper.getUrlById(Integer.parseInt(id));
    }

    @Override
    public List<FunctionDTO> getFunctionByUserId(Integer userId) {
        UserBusiness businessRole = businessService.getBusinessByTypeAndKeyId(BusinessConstance.BUSINESS_USER_ROLE,userId);
        Integer roleId = StringUtils.parseValue(businessRole.getValue())[0];
        UserBusiness businessFunction = businessService.getBusinessByTypeAndKeyId(BusinessConstance.BUSINESS_ROLE_FUNCTIONS,roleId);
        Integer [] funIds = StringUtils.parseValue(businessFunction.getValue());

        MPJLambdaWrapper<Function> childWrapper = new MPJLambdaWrapper();
        childWrapper.in(Function::getId,funIds);

                MPJLambdaWrapper<Function> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(Function::getId,Function::getComponent,Function::getIcon,Function::getText,Function::getUrl);
        queryWrapper.eq(Function::getParentNumber,0);//主表只有1级菜单数据
        queryWrapper.in(Function::getId,funIds);
        queryWrapper.selectCollection(Function.class,FunctionDTO::getChildren);
        queryWrapper.innerJoin(Function.class,"t1",Function::getParentNumber,Function::getNumber);
        queryWrapper.in("t1.id",funIds);
        return functionMapper.selectJoinList(FunctionDTO.class,queryWrapper);
    }
}
